package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 */
public class ZolBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = { "http://bbs.zol.com.cn/[a-z]*/[a-z0-9]*_[a-z0-9]*.html", };
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String updateUrl = null;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("div#bookTitle h1").text();
		date = DateUtils.matchDate(doc.select("span.publish-time").text());
		content = doc.select("div#bookContent").text();
		Elements authorList = doc.select("div.pop-inner");
		if(authorList.size()>1)
		{
			author = authorList.get(0).text();
		}
		
		NewsMeta news = new NewsMeta();
		news.setUrl(url);
		news.setType(4);
		news.setAuthor(author);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(commnetNum);
		news.setClickNum(clickNum);
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setUpdateUrl(updateUrl);
		return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)) {
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	
}
